package minCostClimbingStairs;

public class Solution {
    public int minCostClimbingStairs(int[] cost) {
        // dp[i] 表示到达第i个台阶需要的花费
//        int[]dp = new int[cost.length+1];
//        dp[0] = 0;
//        dp[1] = 0;
//        dp[2] = Math.min(cost[0],cost[1]);
//        for (int i = 3; i <= cost.length ; i++) {
//            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
//        }
//        return dp[dp.length-1];

        // 滚动数组空间复杂度优化
        int prev = 0;
        int curr = 0;
        for (int i = 2; i < cost.length; i++) {
            int next = Math.min(prev+cost[i-2],curr+cost[i-1]);
            prev = curr;
            curr = next;
        }
        return curr;
    }
}
